'''
爬取网址(咸鱼 闲不住)
https://www.goofish.com/search?q=%E6%89%8B%E6%9C%BA&spm=a21ybx.search.searchInput.0

1.请求sign的封装
    搜索 sign: js生成核心代码 i(d.token + "&" + j + "&" + h + "&" + c.data)
2.使用pyexecjs 执行js代码
    js代码来源：在控制台（前提改代码块已打上断点）
    执行：i(d.token + "&" + j + "&" + h + "&" + c.data)
         i
3.将js代码执行结果，拼接给请求params 体，发起请求；

4.改变 pageNumber：页数发起请求；

使用到了 网页F12 请求地址复制cUrl("bash") 在该网址生成爬取代码：https://curlconverter.com/
'''
import requests

cookies = {
    '_samesite_flag_': 'true',
    'cookie2': '17374e90916116a4cc7b9ae95b63b50c',
    't': 'af1212d3f4e8e4fc76a316cd321ffe1b',
    '_tb_token_': '75be85371eb96',
    'cna': 'qr1tH2U1I0wBASQJikSwo0lQ',
    'xlly_s': '1',
    'isg': 'BPT0IahvLsHFI7qN8YUkNYM7xbJmzRi334fC2Y5Vnn8C-ZVDtt6oR4nyeTEhAVAP',
    'mtop_partitioned_detect': '1',
    '_m_h5_tk': '8de5b104cf7ad927fe3bfbdb14912818_1726410757763',
    '_m_h5_tk_enc': '40b109bf07fa06d4da29668ee6800ce1',
    'tfstk': 'fKMSg-_1Dab5avAy1MK4Go8FFy2IO29NAMZKjDBPv8eRJwnTu4zzUHvQJ44qUzozeyBI8Dkr8vihdknK-34eqIooqJ2p7FyBQ0mknGTMNYN-DKEEDTCdnXJkYJ2p7E5Vp7v_KVlaSF-IDnZUAwI-JuFADrq72JU89ZCYXreLJ2FRHiELbgIRe7KjDrqLpyhAwEZDNznWDlT2P7_GD0zfpOsgV7Kn46WCdxZ7Nv38luZShuN7WSWunHH-uDHrESA5wJmq12GKWE5zyfZsl5lW5TgsrknQv49GfrHSAxNogaC8f8a76YF9L9i79jMbUYLhsmoby5wqgIYzT8gSsPVJiE0IczuKFSQWuylZ0YFjWEWj-WGKU-HJkdsPfOzsYC1Cc5XQcP-Xc6fHGYG3BaNTjgP8mokwcn_5t7E0cP-Xc6f3woq4gntfP6f..',
}

headers = {
    'accept': 'application/json',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'cache-control': 'no-cache',
    'content-type': 'application/x-www-form-urlencoded',
    # 'cookie': '_samesite_flag_=true; cookie2=17374e90916116a4cc7b9ae95b63b50c; t=af1212d3f4e8e4fc76a316cd321ffe1b; _tb_token_=75be85371eb96; cna=qr1tH2U1I0wBASQJikSwo0lQ; xlly_s=1; isg=BPT0IahvLsHFI7qN8YUkNYM7xbJmzRi334fC2Y5Vnn8C-ZVDtt6oR4nyeTEhAVAP; mtop_partitioned_detect=1; _m_h5_tk=8de5b104cf7ad927fe3bfbdb14912818_1726410757763; _m_h5_tk_enc=40b109bf07fa06d4da29668ee6800ce1; tfstk=fKMSg-_1Dab5avAy1MK4Go8FFy2IO29NAMZKjDBPv8eRJwnTu4zzUHvQJ44qUzozeyBI8Dkr8vihdknK-34eqIooqJ2p7FyBQ0mknGTMNYN-DKEEDTCdnXJkYJ2p7E5Vp7v_KVlaSF-IDnZUAwI-JuFADrq72JU89ZCYXreLJ2FRHiELbgIRe7KjDrqLpyhAwEZDNznWDlT2P7_GD0zfpOsgV7Kn46WCdxZ7Nv38luZShuN7WSWunHH-uDHrESA5wJmq12GKWE5zyfZsl5lW5TgsrknQv49GfrHSAxNogaC8f8a76YF9L9i79jMbUYLhsmoby5wqgIYzT8gSsPVJiE0IczuKFSQWuylZ0YFjWEWj-WGKU-HJkdsPfOzsYC1Cc5XQcP-Xc6fHGYG3BaNTjgP8mokwcn_5t7E0cP-Xc6f3woq4gntfP6f..',
    'origin': 'https://www.goofish.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.goofish.com/',
    'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
}

params = {
    'jsv': '2.7.2',
    'appKey': '12574478',
    # 't': '1726402564579',
    # 'sign': '968616ec9ff81165d22a3fd4cb4c19d7',
    'v': '1.0',
    'type': 'originaljson',
    'accountSite': 'xianyu',
    'dataType': 'json',
    'timeout': '20000',
    'AntiCreep': 'true',
    'AntiFlool': 'true',
    'api': 'mtop.taobao.idlemtopsearch.pc.search',
    'sessionOption': 'AutoLoginOnly',
    'spm_cnt': 'a21ybx.search.0.0',
    'spm_pre': 'a21ybx.search.searchInput.0',
}

data = {
    'data': '{"pageNumber":2,"keyword":"手机","fromFilter":false,"rowsPerPage":30,"sortValue":"","sortField":"","customDistance":"","gps":"","propValueStr":{},"customGps":""}',
}

data2 = data['data']
# print(data2)

import execjs
# 可以编译一个js文件
with open('./XianYvXianBuZhuCode.js', 'r', encoding='utf-8') as f:
    js_code = f.read()

ctx = execjs.compile(js_code)
result = ctx.call("gainSign", data2)
# print(result)

params['t'] =  result['time']
params['sign'] = result['sign']
# print(params)


response = requests.post(
    'https://h5api.m.goofish.com/h5/mtop.taobao.idlemtopsearch.pc.search/1.0/',
    params=params,
    cookies=cookies,
    headers=headers,
    data=data,
)

print(response.json())